home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13546 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.9 KB  |  76 lines

  1. Path: cypher.3do.com!user
  2. From: tsw@3do.com (Tom Watson)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Can you printf a long
  5. Date: Mon, 08 Apr 1996 13:02:16 -0800
  6. Organization: The 3DO Corporation
  7. Distribution: world
  8. Message-ID: <tsw-0804961302160001@cypher.3do.com>
  9. References: <4ju8o1$dc3@news.netam.net>
  10. NNTP-Posting-Host: cypher.3do.com
  11.  
  12. In article <4ju8o1$dc3@news.netam.net>, bgc@alpha.netam.net (The Bowling
  13. Green Connection) wrote:
  14.  
  15. > I had some trouble with this code:
  16. > (I'm using gcc on Digital Unix)
  17. > int main() {
  18. >     long i;
  19. >     while(1) {
  20. >         i++;
  21. >         printf("%f\n", i);
  22. >     }
  23. >     return 0;
  24. > }
  25. > The numbers printed to the screen as 0.00000.
  26. > (Nevermind that this is an infinite loop--I was piping the output to
  27. > a file which made me run out of disk space. This is just for testing)
  28. > But when I changed %f to %d, the numbers printed correctly.
  29. > I thought that %d had the same limitations as an int..that is,
  30. > it could only print about 4 bits of information, whereas a long
  31. > is capable of more (8 or 16 bits), so wouldn't %f (float) be able
  32. > to better handle those long numbers?  And why did %d work
  33. > correctly, even up to 634,000 (that's when my disk space ran out.. :))
  34. > Well, then I changed the "long i" declaration to "int i", and changed %f 
  35. > to %d, and I got the SAME results!  The numbers went up to 634,000!
  36. > I didn't think an int could handle this much!
  37. > Another strange thing is, I did a sizeof(int) and a sizeof(long), and they
  38. > BOTH returned a 4!!!  That can't be right, can it??!
  39.  
  40. There are a couple of points to ponder here:
  41. 1)  You are trying to print a 'long' with a '%f' specification.  This is
  42. clearly not nice.  The types must correspond.
  43. 2)  The routine 'printf' is a routine that takes a variable number of
  44. arguments, and as such goes thru "the usual argument promotions" for your
  45. system.  While it may not be significant in all cases (read:
  46. implementation defined), usually the argument is promoted to a 'double'
  47. and its size (again: Implementation defined) is probably 8.
  48. 3)  In one machioe that I'm presently working on, arguments are passed
  49. thru registers.  The floating point arguments in floating point registers,
  50. the integers in integer registers.  The value may be fetched from the
  51. incorrect register (which has not been loaded in the case given).
  52. 4)  If you desire to print out floating point numbers that have the same
  53. representation as small integers, you probably want to use a 'union'
  54. construct that will make the storage 'shared'.
  55. 5)  If you have IEEE numbers, the floating point numbers with small
  56. integer representations are of the 'de-normalized' type.
  57. 6)  If you want the structure of the numbers, it may be more meaningful to
  58. use a hex format.
  59. 7)  The '%f' format may not have enough space to give you meaningful
  60. results, use a '%g' format which will give you an exponent for very small
  61. numbers.
  62.  
  63. Lots of problems here!!
  64.  
  65. -- 
  66. Tom Watson
  67. tsw@3do.com         (Home: tsw@johana.com)
  68.